<template>
  <el-row class="warp">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>首页</b></el-breadcrumb-item>
        <el-breadcrumb-item>设置</el-breadcrumb-item>
        <el-breadcrumb-item>修改密码</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>

    <el-col :span="24" class="warp-main">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="oldPwd" label="原密码">
          <el-input type="password" v-model="form.oldPwd"></el-input>
        </el-form-item>
        <el-form-item prop="newPwd" label="新密码">
          <el-input  type="password" v-model="form.newPwd"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPwd" label="确认新密码">
          <el-input  type="password" v-model="form.confirmPwd"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="default" @click="handleChangepwd">提交</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>

import API from '../../api/api_user';
import {bus} from '../../bus.js'


  export default{
    data(){
      var validatePass2 = (rule, value, callback) => {
         if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.form.newPwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        form: {
          oldPwd: '',
          newPwd: '',
          confirmPwd: ''
        },
        rules: {
          oldPwd: [
            {required: true, message: '请输入原密码', trigger: 'blur'}
          ],
          newPwd: [
            {required: true, message: '请输入原密码', trigger: 'blur'}
          ],
          confirmPwd: [
            {validator: validatePass2,required: true, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      handleChangepwd() {
        let that = this;
        that.$refs.form.validate((valid) => {
          if (valid) {
            let user = JSON.parse(window.localStorage.getItem('access-user'));
            that.form.account = user.account;
            API.changepwd(that.form).then(function (result) {
              if (result && parseInt(result.errcode) === 0) {
                //修改成功
                that.$message.success({showClose: true, message: result.errmsg, duration: 2000,onClose:function(){
                  localStorage.removeItem('access-user');
                  that.$router.go('/login'); //用go刷新
                }});
              } else {
                that.$message.error({showClose: true, message: result.errmsg, duration: 2000});
              }
            }, function (err) {
              that.$message.error({showClose: true, message: err.toString(), duration: 2000});
            }).catch(function (error) {
              that.$message.error({showClose: true, message: '请求出现异常', duration: 2000});
            });
            // this.$message({ message: "此功能只是让你看看，不会开发！", duration: 2000});
          }
        })
        
      }
    }
  }
</script>
